package com.code.nowcoder;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/7/28 17:10
 *
 *  素数 与 哥本哈根猜想
 *
 */
public class The86_X {
    public int work (int n, int[] a) {
        // write code here
        int rtn = n;
        for (int i = 1; i < n; i++) {
            int num = a[i] - a[i - 1];
            if(!isValid(num)) {
                if (num % 2 == 1 && num > 15 && !isValid(num - 2)) {
                    rtn += 2;
                } else {
                    rtn++;
                }
            }
        }
        return rtn;
    }

    /**
     * 验证素数的式子
     * @param x
     * @return
     */
    private boolean isValid (int x) {
        if (x == 1 || x == 2 || x == 3) return true;
        if (x % 6 != 1 && x % 6 != 5) return false;
        double temp = Math.sqrt(x);
        for(int i = 5; i <= temp; i+=6) {
            if (x % i == 0 || (x % (i + 2)) == 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        The86_X the86 = new The86_X();
        the86.work(9,new int[]{0,2,7,8,11,16,18,20,28});
    }

}
